
#include "iostream"
#include "vector"
#include "unordered_map"
#include "unordered_set"
#include "algorithm"
#include "string"
#include "list"
#include "string.h"

struct SwapRec{
    std::string s;
    int swap_cnt;
    SwapRec(std::string s,int cnt):s(s),swap_cnt(cnt){}
};
class OneLoop {
public:
//    std::string s1;
    std::unordered_map<std::string,char> sols;
    std::list<SwapRec> queue;
    int swap_at_least=-1;
    void bfs(){
        while (!queue.empty()){
            auto rec=queue.front();
            queue.pop_front();
            //已经记录过
            if(sols.find(rec.s)!=sols.end()){
                continue;
            }
            sols[rec.s]=1;
            if(rec.s.find("2012")!=-1){
                swap_at_least=rec.swap_cnt;
                return;
            }
            for(int i=0;i<rec.s.size()-1;i++){
                //交换
                std::swap(rec.s[i],rec.s[i+1]);
                queue.push_back(SwapRec(rec.s,rec.swap_cnt+1));
                std::swap(rec.s[i],rec.s[i+1]);
            }
        }
    }
    void run(
           int a
    ) {
        std::string s1;
        for(int i=0;i<a;i++){
            char c=std::cin.get();
            s1+=c;
        }
        queue.push_back(SwapRec(s1,0));
        bfs();
//        recur(0);
        printf("%d\n",swap_at_least);
    }
};

class Solution {
public:
    void run() {

        bool first = true;
        int a, b, c, d, e;
        std::string s1, s2;
        while (
                std::cin >> a

                ) {
//            std::getline(std::cin,s2);
            auto cc=std::cin.get();//换行
            OneLoop loop;
            loop.run(a);
        }

    }
};

int main() {
//    bool cur_num= true;
    Solution().run();
    return 0;
}

